//
//  xsAudioConditioningSettingView.m
//  digital_robot_demo
//
//  Created by xskj on 2024/9/9.
// 音频参数

#import "xsAudioConditioningSettingView.h"

@interface xsAudioConditioningSettingView()

@property (nonatomic, strong) UIView *bgView;

@property (nonatomic, strong) UILabel *topTitleLabel;
@property (nonatomic, strong) UIButton *xsXbutton;// x按钮

@property (nonatomic, strong) UILabel  *volumeLabel;// 音量
@property (nonatomic, strong) UISlider *volumeSlider;
@property (nonatomic, strong) UILabel  *volumeNumberLabel;//音量数值

@property (nonatomic, strong) UILabel  *languageSpeedLabel;// 语速
@property (nonatomic, strong) UISlider *languageSpeedSlider;
@property (nonatomic, strong) UILabel  *languageSpeedNumberLabel;//语速数值


@property (nonatomic, strong) UIButton *xsTryListenbutton;// 试听按钮

// musicType 0选择配音中的音频参数 2选择背景音中的音频参数
@property (nonatomic,assign)NSInteger musicType;

@end

@implementation xsAudioConditioningSettingView


// musicType 0选择配音中的音频参数 2选择背景音中的音频参数
- (instancetype)initWithMusicType:(NSInteger)musicType{
    
    if (self = [super init]) {
        self.backgroundColor = UIColor.blackColor;
        
        self.musicType = musicType;
        [self addSubview:self.bgView];
        
        [self.bgView addSubview:self.topTitleLabel];
        [self.bgView addSubview:self.xsXbutton];
        
        [self.bgView addSubview:self.volumeLabel];
        [self.bgView addSubview:self.volumeSlider];
        [self.bgView addSubview:self.volumeNumberLabel];
        
        if(musicType==0){
            [self.bgView addSubview:self.languageSpeedLabel];
            [self.bgView addSubview:self.languageSpeedSlider];
            [self.bgView addSubview:self.languageSpeedNumberLabel];
            
            [self.bgView addSubview:self.xsTryListenbutton];
        }
        
        
    }
    return self;
}



-(void)layoutSubviews{
    [super layoutSubviews];
    //_recorder.preViewLayer.frame = self.frame;
    
    NSLog(@"layoutSubviews--self.bounds==%@",NSStringFromCGRect(self.bounds));
    
    XSLogMethod2(@"layoutSubviews")
}


- (void)updateConstraints{
    [super updateConstraints];
    //
    //    __weak __typeof (&*self)weakSelf = self;
    
    NSLog(@"updateConstraints");
    
    XSLogMethod2(@"updateConstraints")
    
}

- (void)xsChangeFrame{
    
    CGFloat margin15 = 15;
    CGFloat subViewW = self.width-2.0*margin15;
    
//    self.bgView.backgroundColor = UIColor.blackColor;
    
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left).offset(margin15);
        make.top.bottom.equalTo(self);//.offset(10);
        make.right.equalTo(self.mas_right).offset(-margin15);
    }];
    
    
    [self.xsXbutton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.bgView.mas_top).offset(7.5);
        make.right.equalTo(self.bgView.mas_right);
        make.size.mas_equalTo(CGSizeMake(35, 35));
    }];
    
    [self.topTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.bgView);//.offset(10);
        make.top.equalTo(self.bgView.mas_top);
        make.height.mas_equalTo(50);
    }];
    
    
    
    
    [self.volumeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.topTitleLabel.mas_bottom).offset(margin15);
        make.left.equalTo(self.bgView.mas_left);//.offset(margin15);
        make.size.mas_equalTo(CGSizeMake(45, 35));
        
    }];
    [self.volumeNumberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.volumeLabel);
        make.right.equalTo(self.bgView.mas_right);//.offset(margin15);
        make.size.mas_equalTo(CGSizeMake(45, 35));
        
    }];
//    CGFloat volumeSliderW = subViewW - 10;
//    CGFloat volumeSliderY = ;
    [self.volumeSlider mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.volumeLabel);
        make.left.equalTo(self.volumeLabel.mas_right).offset(5);
        make.right.equalTo(self.volumeNumberLabel.mas_left).offset(5);
        make.height.mas_equalTo(16.0);
        
    }];
    
    if(self.musicType==0){
        [self.languageSpeedLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.volumeLabel.mas_bottom).offset(10);
            make.left.equalTo(self.bgView.mas_left);//.offset(margin15);
            make.size.mas_equalTo(CGSizeMake(45, 35));
            
        }];
        [self.languageSpeedNumberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.languageSpeedLabel);
            make.right.equalTo(self.bgView.mas_right);//.offset(margin15);
            make.size.mas_equalTo(CGSizeMake(45, 35));
            
        }];
    //    CGFloat languageSpeedSliderW = subViewW - 10;
    //    CGFloat languageSpeedSliderY = ;
        [self.languageSpeedSlider mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.languageSpeedLabel);
            make.left.equalTo(self.languageSpeedLabel.mas_right).offset(5);
            make.right.equalTo(self.languageSpeedNumberLabel.mas_left).offset(5);
            make.height.mas_equalTo(16.0);
            
        }];
        
        
        [self.xsTryListenbutton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.languageSpeedLabel.mas_bottom).offset(margin15);
            make.left.equalTo(self.languageSpeedLabel.mas_left);//.offset(5);
            make.right.equalTo(self.languageSpeedNumberLabel.mas_right);//.offset(5);
            make.height.mas_equalTo(40.0);
            
        }];
        self.xsTryListenbutton.layer.cornerRadius = 6.0;
        self.xsTryListenbutton.layer.borderWidth = 1.0;
        self.xsTryListenbutton.layer.borderColor = COLOR_textBlueColor.CGColor;
        self.xsTryListenbutton.layer.masksToBounds = YES;
    }
}


- (UIView *)bgView{
    
    if(!_bgView){
        _bgView = [[UIView alloc] init];
        _bgView.backgroundColor = UIColor.clearColor;
    }
    return _bgView;
}

- (UIButton *)xsXbutton{
    if (!_xsXbutton) {
        _xsXbutton = [UIButton buttonWithType:UIButtonTypeCustom];
        _xsXbutton.backgroundColor = UIColor.clearColor;
        [_xsXbutton addTarget:self action:@selector(xsXbuttonAction:) forControlEvents:UIControlEventTouchUpInside];
        [_xsXbutton setImage:[UIImage imageNamed:@"xsBossIPX"] forState:UIControlStateNormal];
    }
    return _xsXbutton;
}
- (void)xsXbuttonAction:(UIButton *)btn{
    NSLog(@"xsXbuttonAction\n\n");
   
    if([self.delegate_AudioConditioningSetting respondsToSelector:@selector(xsAudioConditioningSettingVXbuttonEvent:withMusicType:)]){
        [self.delegate_AudioConditioningSetting xsAudioConditioningSettingVXbuttonEvent:btn withMusicType:self.musicType];
    }
}

- (UILabel *)topTitleLabel {
    if (!_topTitleLabel) {
        _topTitleLabel = [[UILabel alloc] init];
        _topTitleLabel.backgroundColor = UIColor.clearColor;
        _topTitleLabel.font = FontMedium(18);// FontSemibold(17);
        _topTitleLabel.textColor = UIColor.whiteColor;//COLOR_TextGray9B;
        _topTitleLabel.numberOfLines = 1;
        _topTitleLabel.text = @"音频参数";
        _topTitleLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _topTitleLabel;
}

- (UISlider *)volumeSlider{
    
    if(!_volumeSlider){
        // 创建 UISlider
        _volumeSlider = [[UISlider alloc] init];
        _volumeSlider.minimumValue = 0;
        _volumeSlider.maximumValue = 100;
        _volumeSlider.value = 50;
        [_volumeSlider setThumbTintColor:UIColor.grayColor];
        [_volumeSlider setMinimumTrackTintColor:UIColor.blueColor];
        [_volumeSlider setMaximumTrackTintColor:UIColor.lightGrayColor];
        [_volumeSlider addTarget:self action:@selector(volumeSliderValueChangeEvent:) forControlEvents:UIControlEventValueChanged];
    }
    return _volumeSlider;
}

- (void)volumeSliderValueChangeEvent:(UISlider *)sender{
    
    CGFloat sValue = sender.value;
    NSString *tempV = [NSString stringWithFormat:@"volumeSliderValueChangeEvent-sValue==%f",sValue];
    XSLogMethod2(tempV);
    
    _volumeNumberLabel.text = [NSString stringWithFormat:@"%.0f",sValue];
    if([self.delegate_AudioConditioningSetting respondsToSelector:@selector(xsAudioVolumeChange:withMusicType:)]){
        [self.delegate_AudioConditioningSetting xsAudioVolumeChange:sValue withMusicType:self.musicType];
    }
}


- (UILabel *)volumeLabel {
    if (!_volumeLabel) {
        _volumeLabel = [[UILabel alloc] init];
        _volumeLabel.backgroundColor = UIColor.clearColor;
        _volumeLabel.font = Font(16);// FontSemibold(17);
        _volumeLabel.textColor = UIColor.whiteColor;//COLOR_TextGray9B;
        _volumeLabel.numberOfLines = 1;
        _volumeLabel.text = @"音量";
        _volumeLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _volumeLabel;
}


- (UILabel *)volumeNumberLabel {
    if (!_volumeNumberLabel) {
        _volumeNumberLabel = [[UILabel alloc] init];
        _volumeNumberLabel.backgroundColor = UIColor.clearColor;
        _volumeNumberLabel.font = Font(16);// FontSemibold(17);
        _volumeNumberLabel.textColor = UIColor.whiteColor;//COLOR_TextGray9B;
        _volumeNumberLabel.numberOfLines = 1;
        _volumeNumberLabel.text = @"50";
        _volumeNumberLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _volumeNumberLabel;
}



- (UILabel *)languageSpeedLabel {
    if (!_languageSpeedLabel) {
        _languageSpeedLabel = [[UILabel alloc] init];
        _languageSpeedLabel.backgroundColor = UIColor.clearColor;
        _languageSpeedLabel.font = Font(16);// FontSemibold(17);
        _languageSpeedLabel.textColor = UIColor.whiteColor;//COLOR_TextGray9B;
        _languageSpeedLabel.numberOfLines = 1;
        _languageSpeedLabel.text = @"语速";
        _languageSpeedLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _languageSpeedLabel;
}
- (UISlider *)languageSpeedSlider{
    
    if(!_languageSpeedSlider){
        // 创建 UISlider
        _languageSpeedSlider = [[UISlider alloc] init];
        _languageSpeedSlider.minimumValue = 0.5;
        _languageSpeedSlider.maximumValue = 2.0;
        _languageSpeedSlider.value = 1.0;
        [_languageSpeedSlider setThumbTintColor:UIColor.grayColor];
        [_languageSpeedSlider setMinimumTrackTintColor:UIColor.blueColor];
        [_languageSpeedSlider setMaximumTrackTintColor:UIColor.lightGrayColor];
        [_languageSpeedSlider addTarget:self action:@selector(languageSpeedSliderValueChangeEvent:) forControlEvents:UIControlEventValueChanged];
    }
    return _languageSpeedSlider;
}
- (void)languageSpeedSliderValueChangeEvent:(UISlider *)sender{
    
    CGFloat sValue = sender.value;
    NSString *tempV = [NSString stringWithFormat:@"languageSpeedSliderValueChangeEvent-sValue==%f",sValue];
    XSLogMethod2(tempV);
    
    _languageSpeedNumberLabel.text = [NSString stringWithFormat:@"%.2f",sValue];
    if([self.delegate_AudioConditioningSetting respondsToSelector:@selector(xsLanguageSpeedChange:)]){
        [self.delegate_AudioConditioningSetting xsLanguageSpeedChange:sValue];
    }
}

- (UILabel *)languageSpeedNumberLabel {
    if (!_languageSpeedNumberLabel) {
        _languageSpeedNumberLabel = [[UILabel alloc] init];
        _languageSpeedNumberLabel.backgroundColor = UIColor.clearColor;
        _languageSpeedNumberLabel.font = Font(16);// FontSemibold(17);
        _languageSpeedNumberLabel.textColor = UIColor.whiteColor;//COLOR_TextGray9B;
        _languageSpeedNumberLabel.numberOfLines = 1;
        _languageSpeedNumberLabel.text = @"1.0";
        _languageSpeedNumberLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _languageSpeedNumberLabel;
}


- (UIButton *)xsTryListenbutton{
    if (!_xsTryListenbutton) {
        _xsTryListenbutton = [UIButton buttonWithType:UIButtonTypeCustom];
        _xsTryListenbutton.backgroundColor = UIColor.clearColor;
        [_xsTryListenbutton addTarget:self action:@selector(xsTryListenbuttonAction:) forControlEvents:UIControlEventTouchUpInside];
//        [_xsTryListenbutton setImage:[UIImage imageNamed:@"xsBossIPX"] forState:UIControlStateNormal];
        [_xsTryListenbutton setTitle:kLocalizedString(@"试听") forState:UIControlStateNormal];
        [_xsTryListenbutton setTitleColor:COLOR_textBlueColor forState:UIControlStateNormal];
        _xsTryListenbutton.font = Font(16);
    }
    return _xsTryListenbutton;
}
- (void)xsTryListenbuttonAction:(UIButton *)btn{
    NSLog(@"xsTryListenbuttonAction\n\n");
   
    if([self.delegate_AudioConditioningSetting respondsToSelector:@selector(xsTryListenbuttonEvent:)]){
        [self.delegate_AudioConditioningSetting xsTryListenbuttonEvent:btn];
    }
}


@end



